<?php
/**
 * Created by PhpStorm.
 * Author: Coder
 * DateTime: 2019/5/17 14:54
 * Email: 416716328@qq.com
 */
$url = $_GET['url'];
if (strstr($url, "douyin.com")) {
    $str = curl_get($url);
    $id = GetBetween($str, 'video/', '/?');
    $str = curl_get('https://api-hl.amemv.com/aweme/v1/aweme/detail/?aid=1128&app_name=aweme&version_code=251&aweme_id=' . $id);
    $arr = json_decode($str, true);
    $desc = $arr['aweme_detail']['desc'];
    $name = $arr['aweme_detail']['author']['nickname'];
    $avatar = $arr['aweme_detail']['author']['avatar_larger']['url_list'][0];
    $music_url = $arr['aweme_detail']['music']['play_url']['url_list'][0];
    $cover = $arr['aweme_detail']['video']['cover']['url_list'][0];
    $play = $arr['aweme_detail']['video']['play_addr']['url_list'][0];
    exit(json_encode(['code' => 'ok', 'user' => $name, 'avatar' => $avatar, 'desc' => $desc, 'music' => $music_url, 'cover' => $cover, 'play' => $play]));
} else {
    exit(json_encode(['code' => 'error', 'msg' => '抖音分享链接有误，url参数不正确']));
}

function curl_get($url)
{
    $refer = $url;
    $header = array(
        'CLIENT-IP: ' . randIp(),
        'X-FORWARDED-FOR: ' . randIp(),
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.3578.80 Mobile Safari/537.36");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_REFERER, $refer);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

function GetBetween($content, $start, $end)
{
    $r = explode($start, $content);
    if (isset($r[1])) {
        $r = explode($end, $r[1]);
        return $r[0];
    }
    return '';
}

function randIp()
{
    return mt_rand(0, 255) . '.' . mt_rand(0, 255) . '.' . mt_rand(0, 255) . '.' . mt_rand(0, 255);
}